package com.guigu.dealservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.common.utils.R;
import com.guigu.dealservice.entity.UserCollection;
import com.guigu.dealservice.mapper.UserCollectionMapper;
import com.guigu.dealservice.service.UserCollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 收藏表（存储用户收藏的设备信息） 服务实现类
 * </p>
 *
 * @author ysp
 * @since 2025-10-10
 */
@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection> implements UserCollectionService {
    @Autowired
    private UserCollectionMapper userCollectionMapper;
    @Override
    public String addCollection(Long userId, Long equipmentId) {
        //根据用户id和设备id查询收藏
        QueryWrapper<UserCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("equipment_id",equipmentId);
        UserCollection userCollection1 = this.getOne(queryWrapper);
        if(userCollection1==null){
            UserCollection userCollection = new UserCollection();
            userCollection.setUserId(userId);
            userCollection.setEquipmentId(equipmentId);
            userCollection.setCollectionTime(LocalDateTime.now());
            userCollection.setCreateTime(LocalDateTime.now());
            userCollection.setUpdateTime(LocalDateTime.now());
            userCollectionMapper.insert(userCollection);
            return "收藏成功！！！";
        }else return "设备已收藏";

    }

    @Override
    public String deleteCollection(Long userId, Long equipmentId) {
        //根据用户id和设备id删除收藏
        boolean b = this.remove(new QueryWrapper<UserCollection>().eq("user_id", userId).eq("equipment_id", equipmentId));
        if ( b)
            return "取消收藏成功！！！";
        else
            return "取消收藏失败！！！";
    }

    @Override
    public List<UserCollection> queryCollectionByUserId(Integer userId) {
        QueryWrapper<UserCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<UserCollection> userCollections = this.list(queryWrapper);
        if (!userCollections.isEmpty()) {
            return userCollections;
        }
        return null;
    }
}
